package com.gitee.wsl.doc.prosemirror.plugin

import com.gitee.wsl.doc.prosemirror.state.EditorStateConfig
import com.gitee.wsl.doc.prosemirror.state.EditorState
import com.gitee.wsl.doc.prosemirror.state.Transaction

// A plugin spec may provide a state field (under its [`state`](#state.PluginSpec.state) property)
// of this type, which describes the state it wants to keep. Functions provided here are always
// called with the plugin instance as their `this` binding.
interface StateField<T> {
    // Initialize the value of the field. `config` will be the object
    // passed to [`EditorState.create`](#state.EditorState^create). Note
    // that `instance` is a half-initialized state instance, and will
    // not have values for plugin fields initialized after this one.
    fun init(config: EditorStateConfig, instance: EditorState): T

    // Apply the given transaction to this state field, producing a new
    // field value. Note that the `newState` argument is again a partially
    // constructed state does not yet contain the state from plugins
    // coming after this one.
    fun apply(tr: Transaction, value: T, oldState: EditorState, newState: EditorState): T
}

// ALEKSEI: to avoid having json parsing methods implemented as nullable lambdas we just extend StateField interface
interface StateFieldWithJSON<T> : StateField<T> {
    // Convert this field to JSON.
    fun toJSON(value: T): Any?

    // Deserialize the JSON representation of this field. Note that the
    // `state` argument is again a half-initialized state.
    fun fromJSON(config: EditorStateConfig, value: Any, state: EditorState): T?
}